//
//  Top250DatailViewController.m
//  MyMovie
//
//  Created by apple on 16/5/19.
//  Copyright © 2016年 ljp. All rights reserved.
//

#import "Top250DatailViewController.h"
#import "TopDetailTableViewHeader.h"
#import "MovieDetail.h"
#import "Comment.h"
#import "CommentTableViewCell.h"

@interface Top250DatailViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray *commentArray;
}
@property (strong, nonatomic) IBOutlet UITableView *detailTableView;

@end

@implementation Top250DatailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadData];
    [self createSubviews];
}

- (void)loadData {



    commentArray = [[NSMutableArray alloc] init];
    NSDictionary *dic = [JSONDataService loadJSONFielWithName:@"movie_comment"];
    for (NSDictionary *d in dic[@"list"]) {

        Comment *comment = [[Comment alloc] initWithDictionary:d];
        [commentArray addObject:comment];
    }

}

- (void)createSubviews {

    TopDetailTableViewHeader *topDetailTableViewHeader = [[[NSBundle mainBundle] loadNibNamed:@"View" owner:nil options:nil] lastObject];
    NSDictionary * dic = [JSONDataService loadJSONFielWithName:@"movie_detail"];
        MovieDetail *movieDetail = [[MovieDetail alloc] initWithDictionary:dic];

    topDetailTableViewHeader.movieDetail = movieDetail;

    _detailTableView.tableHeaderView = topDetailTableViewHeader;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return commentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentTableViewCell"];
    if (cell == nil) {

        cell = [[[NSBundle mainBundle] loadNibNamed:@"CommentTableViewCell" owner:nil options:nil] lastObject];
    }
    cell.userCommentLable.numberOfLines = 0;
    cell.comment = commentArray[indexPath.row];

    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    Comment *comment = commentArray[indexPath.row];
    NSString *string = comment.content;

    CGSize size = CGSizeMake(KScreenWidth-50-8, 10000);
    UIFont *font = [UIFont systemFontOfSize:17];
    NSDictionary *attribute = @{NSFontAttributeName : font};

    if (comment.isOpen) {

        CGRect rect = [string boundingRectWithSize:size
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                        attributes:attribute
                                           context:nil];
        CGFloat height = rect.size.height + 60;
        if (height < 90) {
            height = 70;
        }
        return height;
    }

    return 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    Comment *comment = commentArray[indexPath.row];
    comment.isOpen = !comment.isOpen;
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
@end
